iT邦幫忙

2022 iThome 鐵人賽

DAY 17
0
Software Development

這些年,我們早該學會的Spring Framework系列 第 30

Day30 - Declarative transaction management (6)

  • 分享至 

  • xImage
  •  

Review

昨日我們講解當Transaction中的屬性Propagation,Propagation講述的是事務中還有事務是要共用同一個事務還是新開事務的故事。

今日我們將介紹如何透過XML的方式來設定Spring declarative transaction management

以xml設定Declarative transaction management

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 加載外部文件   -->
    <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
    <context:component-scan base-package="com.swj"></context:component-scan>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
    </bean>
    <!-- 配置JdbcTemplate   -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
    </bean>
    <!-- transaction management   -->
    <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!-- 註解驅動的transaction management   -->
<!--    <tx:annotation-driven transaction-manager="tx"></tx:annotation-driven>-->
    <!-- xml based transaction management   -->
    <aop:config>
        <aop:pointcut id="txPoint" expression="execution(* com.swj.service.*.*(..))"/>
        <aop:advisor advice-ref="myAdvice"  pointcut-ref="txPoint"></aop:advisor>
    </aop:config>
    <!--  配置transaction管理器  -->
    <tx:advice id="myAdvice" transaction-manager="tx">
        <tx:attributes>
            <!--  指名哪些方法是事務方法  -->
            <tx:method name="transferMoney" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

</beans>

DAO

@Repository
public class AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void addMoney(String accountId, Integer amount) {
        String sql = "UPDATE ACCOUNT SET BALANCE = BALANCE +? WHERE ACCOUNT_ID = ?";
        jdbcTemplate.update(sql, amount, accountId);
    }

    public void minusMoney(String accountId, Integer amount) {
        String sql = "UPDATE ACCOUNT SET BALANCE = BALANCE -? WHERE ACCOUNT_ID = ?";
        jdbcTemplate.update(sql, amount, accountId);
    }

    public Integer queryAccountBalance(String accountId) {
        String sql = "SELECT BALANCE FROM ACCOUNT WHERE ACCOUNT_ID = ?";
        Integer balance = jdbcTemplate.queryForObject(sql, Integer.class,accountId);

        return balance;
    }
}

Service

@Service
public class AccountService {
    @Autowired
    private AccountDao accountDao;

    public void transferMoney(String from, String to , Integer amount) {
        accountDao.addMoney(to,amount);
        accountDao.minusMoney(from,amount);
        int i = 10/0;
        System.out.println("轉帳完成:"+from+" to "+to+" 金額:"+amount);
    }
}
```java
@Test
public void testDay30()  {
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean30.xml");
    AccountService service = ioc.getBean("accountService",AccountService.class);
    service.transferMoney("1","2",100);
}

Result
https://ithelp.ithome.com.tw/upload/images/20221015/20128084DSfADbp3ZP.jpg

Reference


上一篇
Day29 - Declarative transaction management (5)
系列文
這些年,我們早該學會的Spring Framework30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言